package com.piesat.quake.common.ftp;

import cn.hutool.core.util.ObjectUtil;
import com.piesat.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ftpserver.ftplet.*;

import java.io.File;
import java.io.IOException;

/**
 * 监听ftpserver服务上传和下载情况
 *
 * @author wjx
 */
@Slf4j
public class FtpServerListener extends DefaultFtplet {

    /**
     * 开始连接
     *
     * @param session
     * @return
     * @throws FtpException
     * @throws IOException
     */
    @Override
    public FtpletResult onConnect(FtpSession session) throws FtpException, IOException {
        User user = session.getUser();
        if (ObjectUtil.isNotNull(user) && StringUtils.isNotBlank(user.getName())) {
            log.info("尝试使用用户名：{}，密码：{}登录ftpserver..", user.getName(), user.getPassword());
        }
        return super.onConnect(session);
    }

    /**
     * 断开连接
     *
     * @param session
     * @return
     * @throws FtpException
     * @throws IOException
     */
    @Override
    public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException {
        User user = session.getUser();
        if (ObjectUtil.isNotNull(user) && StringUtils.isNotBlank(user.getName())) {
            log.info("用户名：{}关闭ftpserver连接..", user.getName());
        }
        return super.onDisconnect(session);
    }

    /**
     * 开始上传
     *
     * @param session
     * @param request
     * @return
     * @throws FtpException
     * @throws IOException
     */
    @Override
    public FtpletResult onUploadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
        //获取上传文件的上传路径
        String path = session.getUser().getHomeDirectory();
        //自动创建上传路径
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        //获取上传用户
        String name = session.getUser().getName();
        //获取上传文件名
        String filename = request.getArgument();
        log.info("用户：{}，上传文件{}到目录：{}，状态：开始上传...", name, filename, path);
        return super.onUploadStart(session, request);
    }

    /**
     * 上传完成
     *
     * @param session
     * @param request
     * @return
     * @throws FtpException
     * @throws IOException
     */
    @Override
    public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
        //获取上传文件的上传路径
        String path = session.getUser().getHomeDirectory();
        //获取上传用户
        String name = session.getUser().getName();
        //获取上传文件名
        String filename = request.getArgument();

        File file = new File(path + "/" + filename);
        if (file.exists()) {
            log.info("用户：{}，上传文件{}到目录：{}，状态：完成", name, filename, path);
        }
        return super.onUploadEnd(session, request);
    }

    @Override
    public FtpletResult onDownloadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
        return super.onDownloadStart(session, request);
    }

    @Override
    public FtpletResult onDownloadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
        return super.onDownloadEnd(session, request);
    }
}
